//
//  WBHomeViewController.swift
//  仿新浪微博
//
//  Created by 陈强 on 17/2/8.
//  Copyright © 2017年 强. All rights reserved.
//

import UIKit

//定义全局常量，尽量使用private修饰，到处都能访问到
private let cellId = "cellId"


class WBHomeViewController: WBBaseViewController {

    //微博数据数组
    lazy var statusList = [String]()
    
    override func loadData() {
        
        print("开始加载数据")
        
        //模拟延时加载数据 - dispatch_after
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+1) {
            for i in 0..<18 {
                if self.isPullup{
                    //将数据追加到底部
                    self.statusList.append("上拉\(i)")
                }else{
                    //将数据插入数组顶部--尾随闭包 区分语境 要加self
                    self.statusList.insert(i.description, at: 0)
                }
            }
            
            print("开始加载数据")

            //结束刷新控件
            self.refreshControl.endRefreshing()
            //恢复上拉刷新标记
            self.isPullup = false
            //刷新表格
            self.tableView?.reloadData()
        }

    }
    
    
    
    func showFriends() {
        print(#function)
        
        let vc = WBDemoViewController()
//        vc.hidesBottomBarWhenPushed = true

        //push的动作是Nav做的  可以打断点  po navigationController查看
        navigationController?.pushViewController(vc, animated: true)
        
    }
    
}


// MARK: - 表格数据源方法,具体的数据源方法实现，不需要super
extension WBHomeViewController{
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statusList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        //1.取cell
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
        
        //2.设置cell
        cell.textLabel?.text = statusList[indexPath.row]
        
        //3.返回cell
        return cell

    }
    
}


extension WBHomeViewController{
    override func setupUI() {
        super.setupUI()
        //设置导航栏按钮
        navItem.leftBarButtonItem = UIBarButtonItem(title: "好友", target: self, action: #selector(showFriends))
        
        //注册原型cell
        tableView?.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
    }
}
